{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## OS environ "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- os.environ 提供了一种与操作系统的环境变量直接互动的方法。它允许环境变量的读取、修改、删除和新增。\n",
    "- os.getenv 是 Python 标准库 os 模块中的一个函数，用于从操作系统的环境表中获取指定名称的环境变量值。如果环境变量不存在，它还可以返回一个默认值，避免程序出错。\n",
    "    + os.getenv(key, default=None) 函数接受两个参数：\n",
    "        * key：所要检索的环境变量的名称。\n",
    "        * default：如果指定名称的环境变量不存在则返回的默认值（可选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "d:\\Program\\Python\\Python312;d:\\MyConfiguration\\yyf\\AppData\\Roaming\\Python\\Python312\\Scripts;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\windows\\System32\\OpenSSH\\;D:\\Program Files\\GitExtensions\\;d:\\Program Files\\Git\\cmd;D:\\Program\\nvm;D:\\Program\\nodejs;D:\\Program\\nodejs\\;D:\\Program\\nodejs\\node_global;E:\\repository\\apache-maven-3.6.0\\bin\\;D:\\Program\\go\\bin;D:\\wamp64\\bin\\php\\php7.4.33\\;d:\\Program\\composer;D:\\Program Files\\Sublime Text 3\\;;d:\\Program Files (x86)\\Tencent\\微信web开发者工具\\dll;C:\\Program Files\\dotnet\\;d:\\Program\\cursor\\resources\\app\\bin;D:\\Program Files\\Redis\\;D:\\Program\\Python\\Python312\\Scripts\\;D:\\Program\\Python\\Python312\\;D:\\MyConfiguration\\yyf\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\;D:\\MyConfiguration\\yyf\\AppData\\Local\\Programs\\Python\\Python37-32\\;D:\\MyConfiguration\\yyf\\AppData\\Local\\Microsoft\\WindowsApps;D:\\Program Files\\Microsoft VS Code\\bin;D:\\Program\\nvm;D:\\Program\\nodejs;D:\\MyConfiguration\\yyf\\go\\bin;D:\\MyConfiguration\\yyf\\AppData\\Roaming\\Composer\\vendor\\bin;D:\\Program\\Fiddler;D:\\MyConfiguration\\yyf\\.dotnet\\tools;D:\\Program\\cursor\\resources\\app\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\windows\\System32\\OpenSSH\\;D:\\Program Files\\GitExtensions\\;d:\\Program Files\\Git\\cmd;D:\\Program\\nvm;D:\\Program\\nodejs;D:\\Program\\nodejs\\;D:\\Program\\nodejs\\node_global;E:\\repository\\apache-maven-3.6.0\\bin\\;D:\\Program\\go\\bin;D:\\wamp64\\bin\\php\\php7.4.33\\;d:\\Program\\composer;D:\\Program Files\\Sublime Text 3\\;;d:\\Program Files (x86)\\Tencent\\微信web开发者工具\\dll;C:\\Program Files\\dotnet\\;d:\\Program\\cursor\\resources\\app\\bin;D:\\Program Files\\Redis\\;D:\\Program\\Python\\Python312\\Scripts\\;D:\\Program\\Python\\Python312\\;D:\\MyConfiguration\\yyf\\AppData\\Local\\Programs\\Python\\Python37-32\\Scripts\\;D:\\MyConfiguration\\yyf\\AppData\\Local\\Programs\\Python\\Python37-32\\;D:\\MyConfiguration\\yyf\\AppData\\Local\\Microsoft\\WindowsApps;D:\\Program Files\\Microsoft VS Code\\bin;D:\\Program\\nvm;D:\\Program\\nodejs;D:\\MyConfiguration\\yyf\\go\\bin;D:\\MyConfiguration\\yyf\\AppData\\Roaming\\Composer\\vendor\\bin;D:\\Program\\Fiddler;D:\\MyConfiguration\\yyf\\.dotnet\\tools;D:\\Program\\cursor\\resources\\app\\bin\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "##### 1. 读取环境变量\n",
    "import os \n",
    "path = os.getenv('PATH')\n",
    "# print(path)\n",
    "path = os.environ['PATH']\n",
    "# print(path)\n",
    "path1 = os.environ.get('PATH')\n",
    "print(path1)\n",
    "\n",
    "\n",
    "##### 2. 设置环境变量\n",
    "os.environ[\"my_var\"] = \"123\"\n",
    "print(os.environ[\"my_var\"])\n",
    "\n",
    "##### 3. 删除环境变量\n",
    "del os.environ[\"my_var\"]\n",
    "# print(os.environ[\"my_var\"])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. load_dotenv\n",
    "\n",
    "- python-dotenv\n",
    "- 默认情况下，load_dotenv 将在当前目录或父目录中查找 .env 文件，然后加载其中的变量到 os.environ 中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1235\n",
      "2235\n",
      "33535\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv(dotenv_path='.env',override=True)\n",
    "\n",
    "print(os.environ[\"a\"])\n",
    "print(os.environ[\"b\"])\n",
    "print(os.environ.get(\"c\"))\n",
    "print(os.environ.get(\"d\"))\n",
    "print(os.getenv(\"e\"))\n",
    "print(os.getenv(\"f\"))\n",
    "print(os.getenv(\"g\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1235\n",
      "2235\n",
      "33535\n"
     ]
    }
   ],
   "source": [
    "import os \n",
    "os.environ.update({\n",
    "            \"aa\": os.getenv(\"a\"),\n",
    "            \"bb\": os.getenv(\"b\"),\n",
    "            \"cc\": os.getenv(\"c\")\n",
    "        })\n",
    "print(os.environ[\"aa\"])\n",
    "print(os.environ[\"bb\"])\n",
    "print(os.environ[\"cc\"])\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
